package cn.gok.thread;


import javax.swing.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ClassName 创建线程
 * @Description TODO
 * @Author HP
 * @Date 16:18 2022/7/12
 * @Version 1.0
 **/
class MyThread extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("方式1创建线程对象线程执行run方法");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class A implements Runnable {

    @Override
    public void run() {
        System.out.println("方法2");
    }
}

class B implements Callable {
    private Integer i = 0;

    @Override
    public Object call() throws Exception {
        i++;
        Thread.sleep(5000);
        return i;
    }
}


public class 创建线程 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread t1 = new MyThread();
        t1.start();

        A a = new A();
        new Thread(a).start();
        new Thread(() -> System.out.println("lambda简化a方法2")).start();

        Callable<Integer> b = new B();
        FutureTask<Integer> task = new FutureTask<>(b);
        new Thread(task).start();
        System.out.println("task线程执行完毕，获得结果" + task.get());
    }



}

